var LunarCalendar = require("lunar-calendar");
var fs = require("fs");

var lines = 'DATE,YEAR,MONTH,DAY,WEEK,LYEAR,LMONTH,LDAY\n';

function fixZero(n) {
    return (n < 10 ? '0' : '') + n;
}

for (let year = 1980; year < 2040; year++) {
    for (let month = 1; month < 13; month++) {
        let days = LunarCalendar.calendar(year, month).monthData;
        for (let day of days) {
            // console.log('' + day['lunarYear'] + ',' +  day['lunarMonth'] + ',' +  day['lunarDay'] + ',' +   day['lunarLeapMonth']);
            // 闰月处理
            let lunarMonth = day['lunarMonth'];
            let lunarLeapMonth = day['lunarLeapMonth'];
            if (lunarLeapMonth > 0 && lunarMonth > lunarLeapMonth) {
                if (lunarMonth === lunarLeapMonth + 1) {
                    lunarMonth -= 0.5;
                } else {
                    lunarMonth -= 1;
                }
            }

            // 星期几，0为星期日
            let dateStr = '' + day['year'] + '/' + fixZero(day['month']) + '/' + fixZero(day['day']);
            let d = new Date(Date.parse(dateStr));
            let weekDay = (d.getDay() + 6) % 7 + 1; // 星期日由0改为7

            lines += dateStr + ',' + day['year'] + ',' + day['month'] + ',' + day['day'] + ',' + weekDay + ',' + day['lunarYear'] + ',' + lunarMonth + ',' + day['lunarDay'] + '\n';
        }
    }
}

fs.writeFile('./lunar.csv', lines, function (error) {
    if (error) {
        console.log('生成失败')
    } else {
        console.log('生成成功')
    }
});